<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/11
 * Time: 下午3:25
 */

namespace App\Lib;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use Illuminate\Support\Facades\Redis;


class alisms
{
    public function send_verify_sms($code, $mobi, $time, $ccode = '86')
    {
        if ($ccode != '86'){
            $mobi = $ccode . $mobi;
        }
        $accesskeyid = config("oss.accesskeyidsSms");
        $accesskeysecret = config("oss.accesskeysecretSms");
        AlibabaCloud::accessKeyClient($accesskeyid, $accesskeysecret)
            ->regionId('cn-hangzhou')
            ->asGlobalClient();
        try {
            $result = AlibabaCloud::rpcRequest()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'RegionId' => 'cn-hangzhou',
                        'PhoneNumbers' => $mobi,
                        'SignName' => '航语',
                        'TemplateCode' => 'SMS_253080091',
                        'TemplateParam' => json_encode(['code'=>$code])
                    ],
                ])
                ->request();
            //print_r($result->toArray());
            $ret = $result->toArray();
            write_log("SendSms", "send_verify_sms_ali", [$mobi, $ret]);
            if ($ret['Code'] == 'OK'){
                return true;
            }
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
            return false;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
            return false;
        }
        return false;

    }

}